package com.henu.rescue.controller;

import com.henu.rescue.entity.Evaluate;
import com.henu.rescue.entity.StandardType;
import com.henu.rescue.entity.VO.EvaluateVO;
import com.henu.rescue.entity.VO.StandardAverageScore;
import com.henu.rescue.entity.VO.StandardCount;
import com.henu.rescue.service.EvaluateService;
import com.henu.rescue.service.OrderService;
import com.henu.rescue.utils.Page;
import com.henu.rescue.utils.Result;
import com.henu.rescue.utils.ResultEnums;
import com.henu.rescue.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("evaluate/")
@Api("模拟用户评价救援")
@Slf4j
public class EvaluateController {

    @Autowired
    private EvaluateService evaluateService;

    @Autowired
    private OrderService orderService;

    @PostMapping("add")
    @ApiOperation("模拟接入用户评价(添加评价信息)")
    public Result add(@RequestBody List<Evaluate> evaluateList){

        //判单订单号是否全部一致
        int size = evaluateList.size();
        Integer standardCount = evaluateService.getStandardCount();
        if(standardCount != size){
            return new Result(ResultEnums.PARAMS_ERROR);
        }
        for (int i = 1; i < size; i++) {
            Evaluate evaluate = evaluateList.get(i);
            if(!evaluate.getOrderId().equals(evaluateList.get(i-1).getOrderId())){
                return new Result(ResultEnums.PARAMS_ERROR,"评价订单号不一致");
            }
        }
        Evaluate evaluate = evaluateList.get(0);
        //获取评价的数据对应的工单号，检查是否有此工单
        String orderId = evaluate.getOrderId();
        Integer res = orderService.selectById(orderId);
        System.out.println(res);
        if(res != 1){
            return new Result(ResultEnums.PARAMS_ERROR);
        }
        //用户需要评价的工单号没有问题,将评价全部insert
        for (int i = 0; i < size; i++) {
            Evaluate evaluateI = evaluateList.get(i);
            evaluateService.add(evaluateI);
        }

        return new Result(ResultEnums.SUCCESS);
    }


    @PostMapping("getStandard")
    @ApiOperation("给用户展示评价标准")
    public Result getStandard(){
        List<StandardType> standard = evaluateService.getStandard();
        return new Result(ResultEnums.SUCCESS,standard);
    }

    @PostMapping("getAvg")
    @ApiOperation("获取每个标准的平均分")
    public Result getAvg(){
        List<StandardAverageScore> standardAvg = evaluateService.getStandardAverageScores();
        return new Result(ResultEnums.SUCCESS,standardAvg);
    }

    @PostMapping("getCount80")
    @ApiOperation("获取每个标准的>=80的数量")
    public Result getCount80(){
        List<StandardCount> list = evaluateService.getCount80();
        return new Result(ResultEnums.SUCCESS,list);
    }

    @PostMapping("getCount6080")
    @ApiOperation("获取每个标准的>=60&&<80的数量")
    public Result getCount60_80(){
        List<StandardCount> list = evaluateService.getCount60_80();
        return new Result(ResultEnums.SUCCESS,list);
    }

    @PostMapping("getCount60")
    @ApiOperation("获取每个标准的<60的数量")
    public Result getCount60(){
        List<StandardCount> list = evaluateService.getCount60();
        return new Result(ResultEnums.SUCCESS,list);
    }

    @PostMapping("getEvaluate")
    @ApiOperation("分页查询(可提供标准名称、工单id查询   )")
    public Result<EvaluateVO> getEvaluate(@RequestBody Page<EvaluateVO> page){
        page.setSortMethod("desc");
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"evaluate_id"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnums.PARAMS_ERROR.getCode(), "排序参数不合法！");
            }
        }
        page= evaluateService.getAllEvaluateByPage(page);
        return new Result(ResultEnums.QUERY_SUCCESS,page);
    }
}
